'use strict';

/**
 * @name ProcessListCtrl
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('ProcessListCtrl', ['$scope', '$rootScope', '$state', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaFilter', 'MetaSearch', 'MetaSort', 'MetaUtils', function($scope, $rootScope, $state, $stateParams, MetaResources, MetaRemind, MetaFilter, MetaSearch, MetaSort, MetaUtils) {
    $scope.listCtrl = {
        filterConfig: ['area_choice', 'manager', 'origin'],
        filterParams: {},
        searchConfig: {
            sn_like: '编号'
        },
        searchParams: {},
        sortParams: '-created_at',
        tableParams: {}
    };
    $scope.init = function() {
        MetaUtils.initListCtrl($scope, $stateParams);
    };
    $scope.getListData = function(page) {
        var queryParams = {
            '_embed': 'demandSideProcess,technologySideProcess,demand,demand.publisher,demand.leader,technology,technology.publisher,projectManager',
            '_sort': '',
            '_range': '',
            'areas': '',
            'projectManagerId': ''
        };
        var dynamicQueryParams = [];
        MetaUtils.buildQueryParams(page, queryParams, dynamicQueryParams, $scope.listCtrl, $stateParams);
        if ($stateParams.state === "进程终止") {
            queryParams.canceled = true;
        } else {
            queryParams.state = $stateParams.state;
        }
        MetaResources.process.query(
            queryParams,
            function(data, header) {
                MetaUtils.receiveListData($scope.listCtrl, $stateParams, page, data, header);
            },
            function(error) {
                MetaRemind.serverError(error);
            });
    };
    $scope.changeUrl = function() {
        var urlParams = {
            page: $scope.listCtrl.tableParams.currentPage,
            sort: $scope.listCtrl.sortParams,
            showAll: $scope.listCtrl.tableParams.isShowAll
        };
        MetaFilter.encodeURLFilterParams(urlParams, $scope.listCtrl.filterParams);
        MetaSearch.encodeUrlSearchParams(urlParams, $scope.listCtrl.searchParams);
        $state.go('meta.permission.project.process.list', urlParams);
    };
    $scope.init();
}]);
